#import "FirstViewController.h"
#import <objc/runtime.h>


@interface FirstViewController ()

@property (strong, nonatomic) dispatch_queue_t barrierQueue;
@end

@implementation FirstViewController

//- (id)forwardingTargetForSelector:(SEL)aSelector {
//    return self;
//}
//
//- (void)forwardInvocation:(NSInvocation *)anInvocation {
//    
//}
//
//+ (BOOL)resolveClassMethod:(SEL)sel {
//    return YES;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    return;
    dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.dullgrass.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(conCurrentQueue, ^{
        NSLog(@"dispatch 1");
    });
    dispatch_async(conCurrentQueue, ^{
        NSLog(@"dispatch 2");
    });
    dispatch_barrier_async(conCurrentQueue, ^{
        NSLog(@"dispatch barrier");
    });
    dispatch_async(conCurrentQueue, ^{
        NSLog(@"dispatch 3");
    });
    dispatch_async(conCurrentQueue, ^{
        NSLog(@"dispatch 4");
    });
    
    
    return;
    
    _barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t groupGroup = dispatch_group_create();
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC);
    dispatch_queue_t conCurrentGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSLog(@"current task");
    
    dispatch_group_async(groupGroup, conCurrentGlobalQueue, ^{
        
        NSLog(@"并行任务1");
        long isExecuteOver = dispatch_group_wait(groupGroup, delayTime);
        if (isExecuteOver) {
            NSLog(@"wait over1");
        } else {
            NSLog(@"not over");
        }
    });
    dispatch_group_async(groupGroup, conCurrentGlobalQueue, ^{
        NSLog(@"并行任务2");
        long isExecuteOver = dispatch_group_wait(groupGroup, delayTime);
        if (isExecuteOver) {
            NSLog(@"wait over2");
        } else {
            NSLog(@"not over");
        }
    });
    
    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
    NSLog(@"current task");
    dispatch_async(globalQueue, ^{
        dispatch_queue_t applyQueue = dispatch_get_global_queue(0, 0);
        //第一个参数，3--block执行的次数
        //第二个参数，applyQueue--block任务提交到的队列
        //第三个参数，block--需要重复执行的任务
        dispatch_apply(3, applyQueue, ^(size_t index) {
            NSLog(@"current index %@",@(index));
            sleep(1);
        });
        NSLog(@"dispatch_apply 执行完成");
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^{
            NSLog(@"回到主线程更新UI");
        });
    });
    NSLog(@"next task");
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)nsopration {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    
    NSBlockOperation *opration5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:1];
        NSLog(@"sleepForTimeInterval --- 1");
    }];
    
    
    NSBlockOperation *opration3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:3];
        NSLog(@"sleepForTimeInterval --- 3");
    }];
    
    
    NSBlockOperation *opration1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:5];
        NSLog(@"sleepForTimeInterval --- 5");
    }];
    [queue addOperation:opration5];
    [queue addOperation:opration3];
    [queue addOperation:opration1];
    
    [opration3 addDependency:opration1];
    [opration5 addDependency:opration3];
    
    [[NSOperationQueue mainQueue] addOperation:[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:6];
        NSLog(@"sleepForTimeInterval --- 6");
    }]];

}


@end
